<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bannerList" @selection-change="handleSelectionChange">
      <!-- <el-table-column label="应用类型" align="center" prop="appType" :formatter="appTypeFormat" width="200px" /> -->
      <!-- <el-table-column label="显示位置" align="center" prop="positionType" width="130px" :formatter="positionTypeFormat" /> -->
      <!-- <el-table-column label="跳转类型" align="center" prop="bannerType" width="130px" :formatter="bannerTypeFormat" /> -->
      <el-table-column label="标题" align="center" prop="title" width="130px" />
      <el-table-column label="banner图" align="center" prop="">
        <template slot-scope="scope">
          <el-image style="width: 306px; height: 228px" :src="requestUrl + scope.row.imgUrl">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="跳转链接" align="center" prop="linkUrl" />
      <el-table-column label="排序" align="center" prop="sortNum" width="100px" />
      <el-table-column label="上传时间" align="center" prop="lastModifiedTime" width="160px" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150px">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改banner图管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <!-- <el-form-item label="应用类型">
          <el-radio-group v-model="form.appType">
            <el-radio v-for="dict in appTypeOptions" :key="dict.dictValue" :label="parseInt(dict.dictValue)">{{
              dict.dictLabel }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="显示位置">
          <el-radio-group v-model="form.positionType">
            <el-radio v-for="dict in positionTypeOptions" :key="dict.dictValue" :label="parseInt(dict.dictValue)">{{
              dict.dictLabel }}
            </el-radio>
          </el-radio-group>
        </el-form-item> -->
        <!-- <el-form-item label="跳转类型">
          <el-radio-group v-model="form.bannerType">
            <el-radio v-for="dict in bannerTypeOptions" :key="dict.dictValue" :label="parseInt(dict.dictValue)">{{
              dict.dictLabel }}
            </el-radio>
          </el-radio-group>
        </el-form-item> -->
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="图片" prop="imgUrl">
          <el-upload :limit="1" accept=".jpg, .png" :action="upload.url" :headers="upload.headers"
            :file-list="upload.fileList" :on-success="handleFileSuccess" list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>
        <el-form-item label="跳转链接" prop="linkUrl">
          <el-input v-model="form.linkUrl" placeholder="请输入跳转链接" />
        </el-form-item>
        <el-form-item label="排序" prop="sortNum">
          <el-input-number v-model="form.sortNum" :min="1" :max="999" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBanner,
  delBanner,
  addBanner,
  updateBanner,
} from "@/api/admin/sysBanner";
import { getToken } from "@/utils/auth";

export default {
  name: "Banner",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // banner图管理表格数据
      bannerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 类型字典
      appTypeOptions: [
        { dictValue: "1", dictLabel: "菇王今徕app" },
        // { dictValue: "2", dictLabel: "今创投" },
      ],
      positionTypeOptions: [
        { dictValue: "1", dictLabel: "商城" },
        { dictValue: "2", dictLabel: "我的" },
        { dictValue: "3", dictLabel: "任务" },
      ],
      bannerTypeOptions: [
        { dictValue: "1", dictLabel: "H5页面" },
        { dictValue: "2", dictLabel: "APP页面" },
        { dictValue: "3", dictLabel: "图片页面" },
      ],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        type: 1,
      },
      type: "business",
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 上传参数
      upload: {
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/admin/upload/uploadImg/1",
        // 上传的文件列表
        fileList: [],
      },
      requestUrl: this.$img.requestUrl,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    /** 查询banner图管理列表 */
    getList() {
      this.loading = true;
      listBanner(this.queryParams).then((response) => {
        this.bannerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 类型字典翻译
    appTypeFormat(row, column) {
      return this.selectDictLabel(this.appTypeOptions, row.appType);
    },
    // 类型字典翻译
    positionTypeFormat(row, column) {
      return this.selectDictLabel(this.positionTypeOptions, row.positionType);
    },
    // 类型字典翻译
    bannerTypeFormat(row, column) {
      return this.selectDictLabel(this.bannerTypeOptions, row.bannerType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.title = "添加banner图";
      this.upload.fileList = [];
      this.reset();
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.title = "修改banner图";
      this.reset();
      this.open = true;
      this.form = { ...row };
      this.upload.fileList = [];
      this.upload.fileList.push({
        url: this.requestUrl + this.form.imgUrl,
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.bannerId != null) {
            updateBanner(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addBanner(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.bannerId;
      this.$confirm("是否确认删除该banner图?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delBanner(id);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () { });
    },
    // 文件提交处理
    submitUpload() {
      this.$refs.upload.submit();
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.isUploading = false;
      this.form.imgUrl = response.data.list[0].filePath;
      this.msgSuccess(response.msg);
    },
  },
};
</script>
